package cn.hotel.dao;

import cn.hotel.pojo.ClientInformationManagement;
import cn.hotel.utlis.Crud;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClientInformationManagementsDao extends Crud implements ClientInformationManagements {
    @Override
    public List<ClientInformationManagement> cim(String mobilePhone,int current,int numData) {
        String sql = "SELECT * FROM CLIENT_INFORMATION_MANAGEMENT ";
        StringBuffer sbf = new StringBuffer(sql);
        List<Object> data = new ArrayList<>();
        if(mobilePhone != null){
            sbf.append(" WHERE MOBILEPHONE = ?");
            data.add(mobilePhone);
        }
        sbf.append("  LIMIT ?,?");
        data.add(current);
        data.add(numData);
        ResultSet resultSet = getResultSet(sbf.toString(),data);
        List<ClientInformationManagement> clientInformationManagementList = new ArrayList<>();
        ClientInformationManagement clientInformationManagement = null;

        try {
            while (resultSet.next()){
                clientInformationManagement = new ClientInformationManagement();
                clientInformationManagement.setId(resultSet.getLong(1));
                clientInformationManagement.setPassword(resultSet.getString(2));
                clientInformationManagement.setManageName(resultSet.getString(3));
                clientInformationManagement.setSex(resultSet.getString(4));
                clientInformationManagement.setIdentityCard(resultSet.getString(5));
                clientInformationManagement.setMobilePhone(resultSet.getString(6));
                clientInformationManagementList.add(clientInformationManagement);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            this.setClear(resultSet);
        }
        return clientInformationManagementList;
    }

    @Override
    public int amend(ClientInformationManagement clientInformationManagement) {
        return 0;
    }

    @Override
    public int deletes(String identityCard) {
        String sql = "DELETE FROM CLIENT_INFORMATION_MANAGEMENT WHERE IDENTITYCARD = ?";
        List<Object> data = new ArrayList<>();
        data.add(identityCard);
        return getInt(sql,data);
    }

    @Override
    public int sum(String mobilePhone) {
        String sql = "SELECT COUNT(1) FROM CLIENT_INFORMATION_MANAGEMENT ";
        StringBuffer sbf = new StringBuffer(sql);
        List<Object> data = new ArrayList<>();
        if (mobilePhone != null){
            sbf.append(" WHERE MOBILEPHONE = ?");
            data.add(mobilePhone);
        }
        ResultSet resultSet = getResultSet(sbf.toString(),data);
        int num = 0;

        try {
            while (resultSet.next()){
                num = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            this.setClear(resultSet);
        }
        return num;
    }
}
